package com.apes.scm.rbac.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  通用角色申请单
 * Created by lishikun on 2021/4/28 17:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ApplyRole.CONST_TABLE_NAME)
public class ApplyRole extends Model {
    public static final String CONST_TABLE_NAME = "aut_apply_role";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "通用角色申请单")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("角色")
    @JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false)
    private Role role; //角色

    @OneToMany(mappedBy = "applyRole", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ApplyRoleItem> applyRoleItems = new ArrayList<>(); //通用角色申请单明细

    public List<ApplyRoleItem> getApplyRoleItems() {
        if (applyRoleItems.isEmpty() || applyRoleItems == null ) return null;
        for (ApplyRoleItem item : applyRoleItems) {
            item.setApplyRole(this);
        }
        return applyRoleItems;
    }

    @Comment("备注")
    private String remark;

    @States(name = "单据状态", values = {
            @State(id = "sent", name = "新建"),
            @State(id = "approve", name = "审核"),
            @State(id = "finish", name = "完成")
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if (this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    @Comment("转审核人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_approve_id", referencedColumnName = "id")
    private PartyRole transferApproveId;


    @Comment("转审核日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "transfer_approve_date")
    private Date transferApproveDate;


    @Comment("审核人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    private PartyRole approveId;


    @Comment("审核日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "terminal_type_id", referencedColumnName = "id")
    @Comment("终端")
    private Enumeration terminalType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_type_id", referencedColumnName = "id")
    @Comment("分组")
    private Enumeration groupType;
}
